﻿using Pay.Core;
using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Pay.Web.Areas.Admin.Controllers {
    [RouteArea("admin")]
    [RoutePrefix("productTokenMaker")]
    public class ProductTokenMakerController : AdminController {
        private IProductTokenMakerService _iProductTokenMakerService;
        private IProductService _iProductService;
        private IMerchantService _iMerchantService;

        public ProductTokenMakerController(IProductTokenMakerService iProductTokenMakerService, IProductService iProductService, IMerchantService iMerchantService) {
            _iProductTokenMakerService = iProductTokenMakerService;
            _iProductService = iProductService;
            _iMerchantService = iMerchantService;
        }

        [HttpGet]
        public JsonResult Gets(ProductTokenMakerQuery query) {
            var model = _iProductTokenMakerService.GetProductTokenMakers(query);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult GetProducts(GoodsQuery query) {
            query.sale_status = 2;
            query.sell_out = false;
            var tokenMerchant = _iMerchantService.Get(t_RequestMethod.merchant_no);
            query.merchant_id = tokenMerchant.id;
            query.IsTokenMall = true;
            var model = _iProductService.Get(query);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult AddProductTokenMaker(long productId) {
            var result = _iProductTokenMakerService.AddProductTokenMaker(productId);
            return Json(ResultToJson.Json(result > 0 ? "添加成功" : "添加失败", result), JsonRequestBehavior.AllowGet);
        }

        public JsonResult Get(long id) {
            var model = _iProductTokenMakerService.GetForEdit(id);
            if (model.price - model.cost_price != 0 && model.distribution_price.HasValue)
                model.distribution_rate = model.distribution_price.Value / (model.price - model.cost_price);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult Update(product_token_maker_edit_dto dto) {
            var result = _iProductTokenMakerService.UpdateProductTokenMaker(dto);
            return Json(ResultToJson.Json(result > 0 ? " 保存成功" : "未做任何修改", result), JsonRequestBehavior.AllowGet);

        }

        [HttpDelete]
        public JsonResult DeleteProduct(long product_activity_id) {
            var result = _iProductTokenMakerService.DeleteProductTokenMaker(product_activity_id);
            return Json(ResultToJson.Json(result > 0 ? "删除商品成功" : "删除商品失败", result), JsonRequestBehavior.AllowGet);
        }

    }
}